'Amendment Requirement Script
'2/26/07 1:46 pm
Option Strict Off
imports System
imports Humans = PHSRAG.Insight21.Humans
imports BusinessObjects = PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement

module Script
    sub Main()

		if currentProcess.Type = "PPTYPE_AM" then
			
			' this will be null if no initial review...how to handle that?
			Dim latestIRVersion as Humans.ProtocolVersion = Humans.Protocol.GetLatestApplicationVersion (theProtocol.Id, connectionString, false, cachePolicy)
				
			Dim amendmentQuestionnaireAnswers as System.Collections.ArrayList = currentProtVersion.QuestionnaireAnswers
			if amendmentQuestionnaireAnswers.Count > 0 then	
			
				Dim amendmentFormAnswers as System.Collections.ArrayList = nothing
				Dim amendmentAnswer as FormsManagement.Answer = nothing
				Dim showStaff as boolean = false
				
				amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("SponsorProposed")
				if amendmentFormAnswers.Count > 0 then 
					amendmentAnswer = amendmentFormAnswers(0)
					if amendmentAnswer.Value = "Y" then 
						currentProcess.ShowAttachments = true
					end if	
				end if
				
				if not latestIRVersion is nothing then

					Dim oldAnswers as System.Collections.ArrayList = latestIRVersion.QuestionnaireAnswers	
					Dim questionnaireName as string = ""
					Dim questionnaireAnswerName as string = ""
					Dim isMultiple as boolean = false
											
					Dim answer as FormsManagement.Answer
					
					amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Sponsor")
  					questionnaireAnswerName = FormsManagement.Constants.QuestionnaireAnswerDefaultNames.Sponsor_AM
					questionnaireName = "Sponsor Funding"
					isMultiple = true
					if amendmentFormAnswers.Count > 0 then
						currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
					else
						currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
					end if	
					
					' reset multiple flag
					isMultiple = false

					amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("PerformanceSite")
					questionnaireAnswerName = "Performance Sites / Facilities"
					questionnaireName = "Performance Sites"
					if amendmentFormAnswers.Count > 0 then
						amendmentAnswer = amendmentFormAnswers(0)
						if amendmentAnswer.Value = "Y" then 
							currentProcess.ShowAttachments = true
							currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
						else 
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)	
						end if	
					end if
										
					' determine if it's a medical records study or an intervention study
					amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Interven")
					dim notIntervention as boolean = true
					if amendmentFormAnswers.Count > 0 then 
						if amendmentFormAnswers(0).Value = "Y" then 
							notIntervention = false
						end if
						
    					questionnaireAnswerName = "Ancillary Drugs"
						questionnaireName = "Ancillary Drugs"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("AncillaryDrug")
							if amendmentFormAnswers.Count > 0 then 
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else 
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
							end if					
						end if
							
						questionnaireAnswerName = "Study Details"
						questionnaireName = "Study Details"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("StudyDetails")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)					
							end if
						end if
														
 						questionnaireAnswerName = "Ancillary Devices"
						questionnaireName = "Ancillary Devices"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("AncillaryDevice")
							if amendmentFormAnswers.Count > 0 then 
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else 
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
							end if		
						end if
						
  						questionnaireAnswerName = "Diaries"
						questionnaireName = "Diaries"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Diaries")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)					
							end if
						end if
						
			    		questionnaireAnswerName = "Instruments / Questionnaires"
						questionnaireName = "Instruments / Questionnaires"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Instruments")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)					
							end if
						end if

						questionnaireAnswerName = "Nursing Implementation and Planning"
						questionnaireName = "Nursing Implementation and Planning"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Nursing")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)					
							end if
						end if

  						questionnaireAnswerName = "Privacy / Confidentiality"
						questionnaireName = "Privacy / Confidentiality"				
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else  
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Privacy")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)					
							end if
						end if

						questionnaireAnswerName = "Ionizing Radiation"
						questionnaireName = "Ionizing Radiation"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("IonizingRad")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)					
							end if
						end if
						
						questionnaireAnswerName = "Non-Ionizing Radiation"
						questionnaireName = "Non-Ionizing Radiation"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("NonIonizingRad")
							if amendmentFormAnswers.Count > 0 then	
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)					
							end if
						end if
						
						questionnaireAnswerName = "Recruitment"
						questionnaireName = "Recruitment"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Recruitment")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)					
							end if
						end if
						
						questionnaireAnswerName = "Remuneration"
						questionnaireName = "Remuneration"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Remuneration")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)					
							end if
						end if

						questionnaireAnswerName = "Informed Consent"
						questionnaireName = "Informed Consent"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Consent")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
							end if							
						end if
						
						questionnaireAnswerName = "Study Population"
						questionnaireName = "Study Population"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("StudyPop")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
							end if				
						end if
						
						questionnaireAnswerName = "Individuals With Impaired Decision-Making Capacity"
						questionnaireName = "Impaired Decision Making"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Impaired")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
							end if						
						end if
								
						questionnaireAnswerName = "Research Involving Neonates"
						questionnaireName = "Research Involving Neonates"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Neonates")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
							end if		
						end if
						
						questionnaireAnswerName = "Pregnant Women or Human Fetuses"
						questionnaireName = "Pregnant Women or Human Fetuses"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("PregWom")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
							end if	
						end if				
								
						' MULTIPLES	
  						questionnaireAnswerName = FormsManagement.Constants.QuestionnaireAnswerDefaultNames.Drugs
						questionnaireName = "Drugs / Biologics / Dietary Supplements"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							isMultiple = true
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Drugs")
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
							end if		
						end if
						
  						questionnaireAnswerName = FormsManagement.Constants.QuestionnaireAnswerDefaultNames.MedicalDevices
						questionnaireName = "Medical Devices"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Device")
							isMultiple = true
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
							end if			
						end if	
											
						questionnaireAnswerName = FormsManagement.Constants.QuestionnaireAnswerDefaultNames.Children
						questionnaireName = "Children"
						if notIntervention then
							currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
						else 
							amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("Children")
							isMultiple = true
							if amendmentFormAnswers.Count > 0 then
								currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
							else
								currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)		
							end if
						end if
	
						isMultiple = false
						
						dim dataQuestionnaireName as string = "Research Data Repository"
						dim secondaryQuestionnaireName as string = "Secondary Use of Research Samples / Data"
						dim excessQuestionnaireName as string = "Excess Human Material"
						dim healthQuestionnaireName as string = "Health / Medical Records"
						dim tissueQuestionnaireName as string = "Tissue or Sample Repository"

						' All old forms need to be deleted on this version otherwise they will
						' show up on the Forms Page
						amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("StudyType")
						if amendmentFormAnswers.Count > 0 then
							if amendmentFormAnswers(0).Value = "Data" then
								questionnaireAnswerName = dataQuestionnaireName
								questionnaireName = dataQuestionnaireName
								currentProtVersion.DeleteQuestionnaireAnswer(secondaryQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(excessQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(healthQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(tissueQuestionnaireName)	
							else if amendmentFormAnswers(0).Value = "Secondary" then
								questionnaireAnswerName = secondaryQuestionnaireName
								questionnaireName = secondaryQuestionnaireName
								currentProtVersion.DeleteQuestionnaireAnswer(dataQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(excessQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(healthQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(tissueQuestionnaireName)	
							else if amendmentFormAnswers(0).Value = "Excess" then
								questionnaireAnswerName = excessQuestionnaireName
								questionnaireName = excessQuestionnaireName
								currentProtVersion.DeleteQuestionnaireAnswer(dataQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(secondaryQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(healthQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(tissueQuestionnaireName)	
							else if amendmentFormAnswers(0).Value = "Health" then
								questionnaireAnswerName = healthQuestionnaireName
								questionnaireName = healthQuestionnaireName
								currentProtVersion.DeleteQuestionnaireAnswer(dataQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(secondaryQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(excessQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(tissueQuestionnaireName)	
							else if amendmentFormAnswers(0).Value = "Tissue" then
								questionnaireAnswerName = tissueQuestionnaireName
								questionnaireName = tissueQuestionnaireName
								currentProtVersion.DeleteQuestionnaireAnswer(dataQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(secondaryQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(excessQuestionnaireName)	
								currentProtVersion.DeleteQuestionnaireAnswer(healthQuestionnaireName)	
							end if
							currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireAnswerName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)									
						else
							currentProtVersion.DeleteQuestionnaireAnswer(dataQuestionnaireName)	
							currentProtVersion.DeleteQuestionnaireAnswer(secondaryQuestionnaireName)	
							currentProtVersion.DeleteQuestionnaireAnswer(excessQuestionnaireName)	
							currentProtVersion.DeleteQuestionnaireAnswer(healthQuestionnaireName)	
							currentProtVersion.DeleteQuestionnaireAnswer(tissueQuestionnaireName)	
						end if				
					end if ' end if intervention question was answered (count > 0)
				end if ' if not latestIRVersion is nothing then
					
				amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("PerformanceSite")
				if amendmentFormAnswers.Count > 0 then
					amendmentAnswer = amendmentFormAnswers(0)
					if amendmentAnswer.Value = "Y" then 
						currentProcess.ShowAttachments = true
					end if	
				end if

				amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("SubmittingDocuments")
				if  amendmentFormAnswers.Count > 0 then
					amendmentAnswer = amendmentFormAnswers(0)
					if amendmentAnswer.Value = "Y" then 
						currentProcess.ShowAttachments = true
					end if
				end if
				amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("RemoveDocs")
				if amendmentFormAnswers.Count > 0 then
					amendmentAnswer = amendmentFormAnswers(0)
					if amendmentAnswer.Value = "Y" then 
						currentProcess.ShowAttachments = true
					end if
				end if
				
				amendmentFormAnswers = amendmentQuestionnaireAnswers(0).GetAnswers("AmendStaff")
				if amendmentFormAnswers.Count > 0 then
					amendmentAnswer = amendmentFormAnswers(0)
					if amendmentAnswer.Value = "Y" then 
						showStaff = true
					end if
				end if
				currentProcess.ShowStaff = showStaff
									
			end if ' END IF amendmentQuestionnaireAnswers.Count > 0

			' Force pre-loading of all answers to avoid blocking
			Dim qa as FormsManagement.QuestionnaireAnswer = nothing
			For Each qa in currentProtVersion.QuestionnaireAnswers
				Dim a as FormsManagement.Answer = nothing
				For Each a in qa.Answers
					a.ChildAnswers = a.ChildAnswers
				Next
			Next
			currentProtVersion.Save(connectionString)	
			currentProcess.Save(connectionString)
		end if ' END IF currentProcess.Type = "PPTYPE_AM" then
    end sub 
    	
    
end module